package com.example.filestorage.starter.service;

import com.example.filestorage.starter.exception.FileStorageException;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StorageManagerService {

    private final List<StorageService> storageServices;

    public StorageManagerService(List<StorageService> storageServices) {
        this.storageServices = storageServices;
    }

    public String storeFile(MultipartFile file) {
        // Try each storage service in order until one succeeds
        for (StorageService storageService : storageServices) {
            if (storageService.isEnabled()) {
                try {
                    return storageService.storeFile(file);
                } catch (Exception e) {
                    // Log the error and try the next storage service
                    System.err.println("Failed to store file in " + storageService.getClass().getSimpleName() + ": " + e.getMessage());
                }
            }
        }
        
        throw new FileStorageException("Failed to store file in any available storage service");
    }

    public Resource loadFileAsResource(String fileName) {
        // Try each storage service in order until one succeeds
        for (StorageService storageService : storageServices) {
            if (storageService.isEnabled()) {
                try {
                    return storageService.loadFileAsResource(fileName);
                } catch (Exception e) {
                    // Log the error and try the next storage service
                    System.err.println("Failed to load file from " + storageService.getClass().getSimpleName() + ": " + e.getMessage());
                }
            }
        }
        
        throw new FileStorageException("Failed to load file from any available storage service");
    }
    
    public void deleteFile(String fileName) {
        boolean deleted = false;
        // Try to delete from all enabled storage services
        for (StorageService storageService : storageServices) {
            if (storageService.isEnabled()) {
                try {
                    storageService.deleteFile(fileName);
                    deleted = true;
                } catch (Exception e) {
                    // Log the error
                    System.err.println("Failed to delete file from " + storageService.getClass().getSimpleName() + ": " + e.getMessage());
                }
            }
        }
        
        if (!deleted) {
            throw new FileStorageException("Failed to delete file from any available storage service");
        }
    }
    
    public List<String> listFiles() {
        // Try each storage service in order until one succeeds
        for (StorageService storageService : storageServices) {
            if (storageService.isEnabled()) {
                try {
                    return storageService.listFiles();
                } catch (Exception e) {
                    // Log the error and try the next storage service
                    System.err.println("Failed to list files from " + storageService.getClass().getSimpleName() + ": " + e.getMessage());
                }
            }
        }
        
        // If no storage service is available or all failed, return empty list
        return new ArrayList<>();
    }
}